#include "CTitleBar.h"
#include <QHBoxLayout>
#include <QDebug>
CTitleBar::CTitleBar(QWidget *parent) : QWidget(parent)
{
    initUI();
}
void CTitleBar::initUI()
{

   //禁止父窗口影响子窗口样式,必须加上，否则样式不会起作用
   setAttribute(Qt::WA_StyledBackground);
   this->setFixedHeight(64+10);
   this->setStyleSheet("background-color:rgb(54,54,54)");
   _plogoLabel = new QLabel(this);
   _plogoLabel->setFixedSize(64,64);
   _plogoLabel->setStyleSheet("background-image:url(:/image/image/shexiangtou .png);border:none");

   _ptitleTextLabel = new QLabel(this);
   _ptitleTextLabel->setText("天穹监控");
   _ptitleTextLabel->setFixedWidth(120);
   _ptitleTextLabel->setStyleSheet("QLabel{font-family: Microsoft YaHei;""font-size:24px;""color:#BDC8E2;background-color:rgb(54,54,54);}");

   _psetButton = new QPushButton(this);
   _psetButton->setFixedSize(32, 32);
   _psetButton->setStyleSheet("QPushButton{background-image:url(:/image/image/shezhi.png);"
                              "border:none}""QPushButton:hover{""background-color:rgb(99, 99, 99);"
                              "background-image:url(:/image/image/shezhi.png);border:none;}");
   _pminButton = new QPushButton(this);
   _pminButton->setFixedSize(32, 32);
   _pminButton->setStyleSheet("QPushButton{background-image:url(:/image/image/suoxiao.png);"
                              "border:none}""QPushButton:hover{background-color:rgb(99, 99, 99);"
                              "background-image:url(:/image/image/suoxiao.png);border:none;}");
   QPixmap testPix(":/image/image/suoxiao.png");  // 使用你按钮中的图片路径
   if (testPix.isNull()) {
       qDebug() << "最小化按钮图片加载失败！路径错误或文件不存在";
   } else {
       qDebug() << "图片加载成功，尺寸：" << testPix.size();
   }

   _pbackplayButton=new QPushButton(this);
    _pbackplayButton->setFixedSize(64, 64);
    _pbackplayButton->setStyleSheet(
            "background-image:url(:/image/image/shipinhuifang.png);border:none");
   _pmonitorButton = new QPushButton(this);
   _pmonitorButton->setFixedSize(64, 64);
   _pmonitorButton->setStyleSheet(
           "background-image:url(:/image/image/jiankong.png);border:none");
_pmaxButton = new QPushButton(this);
_pmaxButton->setFixedSize(32, 32);
_pmaxButton->setStyleSheet(
"QPushButton{background-image:url(:/image/image/Frame.png);border:none}"
"QPushButton:hover{"
     "background-color:rgb(99, 99, 99);"
     "background-image:url(:/image/image/Frame.png);border:none;}");
_pcloseButton = new QPushButton(this);
_pcloseButton->setFixedSize(32, 32);
_pcloseButton->setStyleSheet(
"QPushButton{background-image:url(:/image/image/guanbi.png);border:none}"
"QPushButton:hover{"
     "background-color:rgb(99, 99, 99);"
     "background-image:url(:/image/image/guanbi.png);border:none;}");
QHBoxLayout * phLayout = new QHBoxLayout(this);
phLayout->addWidget(_plogoLabel);
phLayout->addWidget(_ptitleTextLabel);
phLayout->addStretch();//添加一个弹簧
phLayout->addWidget(_pmonitorButton);        // 添加监控
phLayout->addWidget(_pbackplayButton);        // 回放
phLayout->addWidget(_psetButton);
//  QSpacerItem* pItem1 = new QSpacerItem(20, 20, QSizePolicy::Fixed, QSizePolicy::Fixed);
//  phLayout->addSpacerItem(pItem1);
phLayout->addWidget(_pminButton);
//  QSpacerItem* pItem2 = new QSpacerItem(18, 20, QSizePolicy::Fixed, QSizePolicy::Fixed);
//  phLayout->addSpacerItem(pItem2);
phLayout->addWidget(_pmaxButton);
//  QSpacerItem* pItem3 = new QSpacerItem(18, 20, QSizePolicy::Fixed, QSizePolicy::Fixed);
//  phLayout->addSpacerItem(pItem3);
phLayout->addWidget(_pcloseButton);
phLayout->setContentsMargins(5,5,5,5);
//this->setLayout(phLayout);
connect(_pminButton, &QPushButton::clicked, this, &CTitleBar::onClickedSlot);
connect(_pmaxButton, &QPushButton::clicked, this, &CTitleBar::onClickedSlot);
connect(_pcloseButton, &QPushButton::clicked, this, &CTitleBar::onClickedSlot);
//connect(_pmonitorButton, &QPushButton::clicked, this, &CTitleBar::onClickedSlot);
//connect(_pbackplayButton, &QPushButton::clicked, this, &CTitleBar::onClickedSlot);暂未实现
}
// 实现按钮点击事件处理
void CTitleBar::onClickedSlot()
{
   QPushButton * pbtn = qobject_cast<QPushButton*>(sender());
   QWidget * pwindow = this->window();
   if(pbtn == _pminButton) {
       pwindow->showMinimized();
   } else if(pbtn == _pmaxButton) {
       if(pwindow->isMaximized()) {
           pwindow->showNormal();
           _pmaxButton->setStyleSheet(
                   "QPushButton{background-image:url(:/image/image/Frame.png);border:none}"
                   "QPushButton:hover{""background-color:rgb(99, 99, 99);"
                    "background-image:url(:/image/image/Frame.png);border:none;}");
       } else  {
           pwindow->showMaximized();
           _pmaxButton->setStyleSheet(
                   "QPushButton{background-image:url(:/image/image/Frame.png);border:none}"
                   "QPushButton:hover{"
                           "background-color:rgb(99, 99, 99);"
                           "background-image:url(:/image/image/Frame.png);border:none;}");
       }
   } else if(pbtn == _pcloseButton) {
       emit sigClose();//发射信号，通知父窗口关闭
   }
}
